package edu.city.class_2201_sno_202212008.Adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import edu.city.class_2201_sno_202212008.Activity.QuestionActivity;
import edu.city.class_2201_sno_202212008.R;
import edu.city.class_2201_sno_202212008.databinding.ViewholderQuestionBinding;

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.Viewholder> {
    private final String correctAnswer;
    private final List<String> users;
    private final QuestionActivity returnScore;
    private int selectedPosition = -1;
    private boolean hasAnswered = false;

    public QuestionAdapter(String correctAnswer, List<String> users, QuestionActivity returnScore) {
        this.correctAnswer = correctAnswer;
        this.users = users;
        this.returnScore = returnScore;
    }

    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ViewholderQuestionBinding binding = ViewholderQuestionBinding.inflate(inflater, parent, false);
        return new Viewholder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull Viewholder holder, @SuppressLint("RecyclerView") int position) {
        ViewholderQuestionBinding binding = holder.binding;
        binding.answerTxt.setText(users.get(position));

        // 设置选中/未选中状态
        if (selectedPosition == position) {
            if (getAnswerIndex(correctAnswer) == position) {
                binding.answerTxt.setBackgroundResource(R.drawable.green_bg);
                binding.answerTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ContextCompat.getDrawable(binding.getRoot().getContext(), R.drawable.tick), null);
            } else {
                binding.answerTxt.setBackgroundResource(R.drawable.red_bg);
                binding.answerTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ContextCompat.getDrawable(binding.getRoot().getContext(), R.drawable.thieves), null);
            }
        } else {
            binding.answerTxt.setBackgroundResource(R.drawable.answer_white);
            binding.answerTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null);
        }

        // 只允许答一次
        binding.getRoot().setOnClickListener(v -> {
            if (!hasAnswered) {
                selectedPosition = position;
                hasAnswered = true;
                if (getAnswerIndex(correctAnswer) == position) {
                    returnScore.amount(5, getAnswerChar(position));
                } else {
                    returnScore.amount(0, getAnswerChar(position));
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    private int getAnswerIndex(String answer) {
        switch (answer) {
            case "a": return 0;
            case "b": return 1;
            case "c": return 2;
            case "d": return 3;
            default: return -1;
        }
    }

    private String getAnswerChar(int position) {
        switch (position) {
            case 0: return "a";
            case 1: return "b";
            case 2: return "c";
            case 3: return "d";
            default: return "";
        }
    }

    public static class Viewholder extends RecyclerView.ViewHolder {
        ViewholderQuestionBinding binding;
        public Viewholder(ViewholderQuestionBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
} 